﻿using System.Linq;
using Fee.Orders.PreWorkflow;
using Identity;
using JwtWebApi.Data;
using JwtWebApi.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace JwtWebApi.Controllers.Manage
{
    [Authorize]
    [Route("manage/[controller]")]
    [ApiController]
    [EnableCors]
    public class DistrictController : Controller
    {
        private readonly ApplicationDbContext _context;

        public DistrictController(ApplicationDbContext context)
        {

            _context = context;
          
        }

        [HttpGet("list")]
        public IActionResult Index()
        {
            var query = _context.Districts.ToList();


            return Json(new
            {
                data = query,
                total = query.Count()
            });
        }

        [HttpPost("Save")]
        [ModelValidteFilter]
        public IActionResult Save([FromBody] District district)
        {
            _context.Update(district);
            _context.SaveChanges();
            return Json(new { successed = true, message = "成功" });
        }

        [HttpGet("get/{id}")]
        public IActionResult Get(int id)
        {
            var district = _context.Districts.FirstOrDefault(_ => _.Id == id);
            return Json(district);
        }
    }
}